<?php
// 应用公共文件


//接口成功返回
if (!function_exists("success")) {
  function success($msg, $data, $code = 200)
  {
    return json([
      "code" => $code,
      "msg" => $msg,
      "data" => $data
    ]);
  }
}
//接口失败返回
if (!function_exists("error")) {
  function error($msg, $code = 500)
  {
    return json([
      "code" => $code,
      "msg" => $msg
    ]);
  }
}

if (!function_exists("buildTree")) {
  function buildTree($list, $pid = 0, &$grouped = null)
  {
    if (!is_array($list)) {
      return [];
    }
    if ($grouped == null) {
      $grouped = [];
      foreach ($list as $value) {
        $grouped[$value['pid']][] = $value;
      }
    }
    $data = [];
    foreach ($grouped[$pid] ?? [] as $value) {
      if (isset($grouped[$value['id']])) {
        $value['children'] = buildTree($list, $value['id'], $grouped);
      }
      $data[] = $value;
    }
    return $data;
  }
}
